package com.voicecomm.database;

/**
 * 数据库配置类
 * 包含数据库连接和连接池的配置参数
 * 
 * @author VoiceComm Team
 * @version 1.0.0
 */
public class DatabaseConfig {
    
    // 数据库连接配置
    private String jdbcUrl;
    private String username;
    private String password;
    private String driverClassName;
    
    // 连接池配置
    private int maxPoolSize = 10;
    private int minIdle = 2;
    private long connectionTimeout = 30000; // 30秒
    private long idleTimeout = 600000; // 10分钟
    private long maxLifetime = 1800000; // 30分钟
    private long leakDetectionThreshold = 60000; // 1分钟
    
    /**
     * 默认构造函数
     */
    public DatabaseConfig() {
        // 默认MySQL配置
        this.driverClassName = "com.mysql.cj.jdbc.Driver";
    }
    
    /**
     * 构造函数
     * 
     * @param jdbcUrl JDBC连接URL
     * @param username 用户名
     * @param password 密码
     */
    public DatabaseConfig(String jdbcUrl, String username, String password) {
        this();
        this.jdbcUrl = jdbcUrl;
        this.username = username;
        this.password = password;
    }
    
    /**
     * 创建MySQL配置
     * 
     * @param host 数据库主机
     * @param port 数据库端口
     * @param database 数据库名
     * @param username 用户名
     * @param password 密码
     * @return 数据库配置对象
     */
    public static DatabaseConfig createMySQLConfig(String host, int port, String database, 
                                                 String username, String password) {
        String jdbcUrl = String.format("jdbc:mysql://%s:%d/%s?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8",
                                     host, port, database);
        return new DatabaseConfig(jdbcUrl, username, password);
    }
    
    /**
     * 创建本地MySQL配置
     * 
     * @param database 数据库名
     * @param username 用户名
     * @param password 密码
     * @return 数据库配置对象
     */
    public static DatabaseConfig createLocalMySQLConfig(String database, String username, String password) {
        return createMySQLConfig("localhost", 3306, database, username, password);
    }
    
    // Getter和Setter方法
    public String getJdbcUrl() {
        return jdbcUrl;
    }
    
    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getDriverClassName() {
        return driverClassName;
    }
    
    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
    
    public int getMaxPoolSize() {
        return maxPoolSize;
    }
    
    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }
    
    public int getMinIdle() {
        return minIdle;
    }
    
    public void setMinIdle(int minIdle) {
        this.minIdle = minIdle;
    }
    
    public long getConnectionTimeout() {
        return connectionTimeout;
    }
    
    public void setConnectionTimeout(long connectionTimeout) {
        this.connectionTimeout = connectionTimeout;
    }
    
    public long getIdleTimeout() {
        return idleTimeout;
    }
    
    public void setIdleTimeout(long idleTimeout) {
        this.idleTimeout = idleTimeout;
    }
    
    public long getMaxLifetime() {
        return maxLifetime;
    }
    
    public void setMaxLifetime(long maxLifetime) {
        this.maxLifetime = maxLifetime;
    }
    
    public long getLeakDetectionThreshold() {
        return leakDetectionThreshold;
    }
    
    public void setLeakDetectionThreshold(long leakDetectionThreshold) {
        this.leakDetectionThreshold = leakDetectionThreshold;
    }
    
    @Override
    public String toString() {
        return String.format("DatabaseConfig{jdbcUrl='%s', username='%s', driverClassName='%s', " +
                           "maxPoolSize=%d, minIdle=%d, connectionTimeout=%dms}", 
                           jdbcUrl, username, driverClassName, maxPoolSize, minIdle, connectionTimeout);
    }
}
